package com.lab02.spring.firstpage.persistence;

import com.lab02.spring.firstpage.models.Player;
import com.lab02.spring.firstpage.models.Reservation;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bsceran
 * Date: 5/23/13
 * Time: 2:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReservationDaoImpl extends SimpleJdbcDaoSupport implements ReservationDao {
    @Override
    public List<Reservation> findAll() {
        String sql = "SELECT * FROM RESERVATION r, PLAYER p WHERE p.NAME = r.PLAYER_NAME";

        List<Reservation> reservations = getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Reservation.class));
        List<Player> players = getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Player.class));
        for(int i=0;i<reservations.size();i++) {
            reservations.get(i).setPlayer(players.get(i));
        }
        return reservations;
    }
}
